@echo off
setlocal enableextensions enabledelayedexpansion

rem Find version number from resource file
for /f "tokens=3" %%a in ('type kbddvp.rc ^| find "ProductVersion"') do set ver=%%~a
for /f "tokens=1-3 delims=.,\" %%a in ("%ver%") do (
    set major=%%a
    set major=!major:"=!
    for %%x in (!major!) do set major=%%x
    set minor=%%b
    set minor=!minor:"=!
    for %%y in (!minor!) do set minor=%%y
    set build=%%c
    set build=!build:"=!
    for %%z in (!build!) do set build=%%z
)

rem Do this outside Makefile since NMake has problems extracting version numbers
del /q kbddvp-!major!_!minor!_!build!-i386.exe 2>NUL
del /q kbddvp-!major!_!minor!_!build!-src-winnt.cab 2>NUL

if /i not "%1"=="clean" (
   if not exist kbddvp.exe (
      call build-layout.bat all
   )
   if exist kbddvp.exe (
      copy /y kbddvp.exe kbddvp-!major!_!minor!_!build!-i386.exe
   )

   echo .OPTION EXPLICIT > build-installer.ddf
   echo .Set CabinetNameTemplate=kbddvp-!major!_!minor!_!build!-src-winnt.cab >> build-installer.ddf
   echo .set DiskDirectoryTemplate= >> build-installer.ddf
   echo .Set CompressionType=LZX >> build-installer.ddf
   echo .Set CompressionMemory=21 >> build-installer.ddf
   echo .Set UniqueFiles="OFF" >> build-installer.ddf
   echo .Set Cabinet=on >> build-installer.ddf
   echo build-layout.bat >> build-installer.ddf
   echo build-installer.bat >> build-installer.ddf
   echo Makefile >> build-installer.ddf
   echo sources >> build-installer.ddf
   echo README.txt >> build-installer.ddf
   echo launcher.c >> build-installer.ddf
   echo makecab-dir.ddf >> build-installer.ddf
   echo Programmer-Dvorak-License.htm >> build-installer.ddf
   for %%a in (c def inf mak rc reg sed zap) do echo kbddvp.%%a >> build-installer.ddf
   makecab /F build-installer.ddf
)
endlocal
